﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;

#if net35

namespace LucyGridTest.net45
{
	/// <summary>
	/// Summary description for UnitTest1
	/// </summary>
	[TestClass]
	public class UnitTest1
	{
		public UnitTest1()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[TestMethod]
		public void net45_IO_Stream()
		{
			var str = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi scelerisque enim nisl, nec tristique nibh commodo ut. 
						Maecenas vitae sem a leo porta fermentum. Nunc commodo ullamcorper condimentum. Proin placerat, diam placerat ornare 
						lobortis, ligula ligula ullamcorper risus, vel posuere leo lectus eu sem. Phasellus viverra eros sed nibh interdum, 
						vitae sagittis eros rhoncus. Sed eget lorem venenatis urna hendrerit semper. Proin scelerisque, felis sed eleifend 
						maximus, risus leo euismod tellus, at porta enim odio auctor nulla. Integer eget felis id leo iaculis hendrerit.";
			var data = UTF8Encoding.UTF8.GetBytes(str);
			var ms = new MemoryStream();
			ms.Write(data, 0, data.Length);
			ms.Seek(0, SeekOrigin.Begin);
			var ms2 = new MemoryStream();
			ms.CopyTo(ms2);
			ms2.Seek(0, SeekOrigin.Begin);
			var str2 = UTF8Encoding.UTF8.GetString(ms2.ToArray());
			Assert.AreEqual(str, str2);
		}

		[TestMethod]
		public void net45_Linq_AsParallel()
		{
			var res = Enumerable.Range(0, 3)
				.AsParallel()
				.Select(i => ++i)
				.ToArray();
			Assert.AreEqual(res.Length, 3);
		}

		[TestMethod]
		public void net45_Threading_Tasks()
		{
			var t = Task.Run(() => { });
			Assert.IsTrue(t.Wait());
			Assert.IsTrue(Task.WaitAll(new Task[]{t}, 1000));
		}

		[TestMethod]
		public void net45_Threading_Wait()
		{
			var t = Task.Run(() => {
				Thread.Sleep(10000);
			});
			Assert.IsTrue(Task.WaitAll(new Task[] { t }, 1000));
		}

		[TestMethod]
		public void net45_Process_Wait()
		{
			var t = Task.Run(() =>
			{
				var p = Process.Start(@"cmd", "/C echo");
				p.WaitForExit();
			});
			Assert.IsTrue(t.Wait(1000));

		}

	}
}
#endif